package com.lc.sell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lc.sell.entity.Merchant;
import com.lc.sell.service.serviceImpl.MerchantServiceImpl;
import com.lc.sell.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙晨
 * @since 2022-02-01
 */
@RestController
@Api(tags = "商家管理")
@RequestMapping("merchant")
public class MerchantController {

    @Autowired
    private MerchantServiceImpl merchantService;

    @GetMapping
    public ModelAndView toMerchant(){
        return new ModelAndView("page/merchant_list");
    }

    @GetMapping("toMerchantEdit")
    public ModelAndView toMerchantEdit(@RequestParam(required = false) Integer id){
        ModelAndView model = new ModelAndView();
        model.setViewName("page/merchant_edit");
        model.addObject("merchant",merchantService.getById(id));
        return model;
    }


    @ApiOperation(value = "修改个人信息页")
    @GetMapping("userinfo")
    public ModelAndView toUserinfo(@RequestParam(value = "mid")Integer mid){
        Merchant one = merchantService.getOne(new QueryWrapper<Merchant>().eq("id", mid));
        ModelAndView mv = new ModelAndView();
        mv.addObject("merchant",one);
        mv.setViewName("page/userinfo");
        return mv;
    }

    @ApiOperation(value = "查询所有商家")
    @GetMapping("findAll")
    public ResultVo findAll(@RequestParam(defaultValue = "1",name = "page") long current, @RequestParam(defaultValue = "10") long size){
        return merchantService.findAllMerchant(current, size);
    }

    @ApiOperation(value = "根据条件查询商家信息")
    @GetMapping("findByMerchant")
    public ResultVo findByMerchant(Merchant merchant,@RequestParam(name = "start") String one,@RequestParam(name = "end") String two){
        return merchantService.findByMerchant(merchant, one, two);
    }

    @ApiOperation(value = "查询所有商家所有商品")
    @GetMapping("findAllGoods")
    public ResultVo findAllGoods(){
        return merchantService.findAll();
    }

    @ApiOperation(value = "根据商家号查询商家")
    @GetMapping("findById")
    public ResultVo findById(Integer id){
        return merchantService.findByMerchantId(id);
    }

    @ApiOperation(value = "添加商家")
    @PostMapping("addMerchant")
    public ResultVo addMerchant(Merchant merchant){
        return merchantService.addMerchant(merchant);
    }

    @ApiOperation(value = "删除商家")
    @DeleteMapping("deleteMerchant")
    public ResultVo deleteMerchant(Integer id){
        return merchantService.deleteMerchant(id);
    }

    @ApiOperation(value = "修改商家")
    @PutMapping("updateMerchant")
    public ResultVo updateMerchant(Merchant merchant){
        return merchantService.updateMerchant(merchant);
    }

    @ApiOperation(value = "隐藏/显示商家")
    @PostMapping("showMerchant")
    public ResultVo showMerchant(Integer id){
        return merchantService.showMerchant(id);
    }
}
